package cn.blue.org.controller;
import cn.blue.base.config.BussinessException;
import cn.blue.base.query.BaseQuery;
import cn.blue.base.util.AjaxResult;
import cn.blue.base.util.LoginContext;
import cn.blue.base.util.PageList;
import cn.blue.org.domain.Employee;
import cn.blue.org.service.EmployeeService;
import cn.blue.user.domain.Logininfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/emp")
public class EmployeeController {
    @Autowired
    private EmployeeService employeeService;
    //删除
    @DeleteMapping("/{id}")
    public AjaxResult delete(@PathVariable("id") Long id) {
        if(id==null){
            throw new BussinessException("参数不能为空");
        }
        else {
            employeeService.del(id);
            return AjaxResult.me().setSuccess(true).setMsg("请求成功");
        }
    }

    //新增或者修改
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody Employee employee) {
        System.out.println(employee);
        if (null == employee.getId()) {
            employeeService.add(employee);
        }else {
            employeeService.update(employee);
        }
        return AjaxResult.me().setSuccess(true).setMsg("请求成功");


    }
    @ResponseBody
    @RequestMapping
    public PageList<Employee>queryPage(@RequestBody BaseQuery query){
        PageList<Employee> list = employeeService.queryByPage(query);
        return list;


    }

    //批量删除
    @PatchMapping
    public AjaxResult batchDel(@RequestBody List<Long>ids){
        System.out.println(ids);
        employeeService.batchDel(ids);
        return AjaxResult.me().setSuccess(true).setMsg("删除成功");

    }

    //部门数据
    @GetMapping
    public List<Employee> findAll(){
        return employeeService.findAll();
    }


    /**
     * 根据shopId查询员工集合
     * @return
     */
    @GetMapping("/selectByShopId")
    public List<Employee>selectByShopId (HttpServletRequest request){
        //获取当前登陆人的店铺id
        Logininfo logininfo = LoginContext.getLogininfo(request);
        Employee employee = employeeService.selectBylogininfo(logininfo.getId());
        Long shopId = employee.getShopId();
        return employeeService.selectByShopId (shopId);
    }


}

